import React, { useImperativeHandle, useRef, forwardRef } from "react";
// useImperativeHandle在父组件里面去获取子组件的属性和方法

const Child = forwardRef((props, ref) => {
  useImperativeHandle(ref, () => ({
    childMethod: () => {
      console.log("childMethod");
    },
    a: 1,
  }));

  return (
    <>
      <div>child</div>
    </>
  );
});

const App = () => {
  const child = useRef();

  const fn = () => {
    child.current.childMethod();
    console.log(child.current.a);
  };

  return (
    <>
      <h2>useImperativeHandle</h2>
      <button onClick={fn}>btn</button>
      <Child ref={child} />
    </>
  );
};

export default App;
